package com.lc.projects.thread.part1;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.Thread.State;

import org.junit.Test;

public class Test01 {
	@Test
	public void testCreate(){
		for (int i = 0; i < 3; i++) {
			Calculator cal = new Calculator(i);
			Thread thread = new Thread(cal);
			thread.start();
			
			Thread thread2 = new Calculator2(i+10);
			thread2.start();
		}
	}
	
	@Test
	public void testGetInfo(){
		Thread threads[] = new Thread[10];
		Thread.State status[] = new Thread.State[10];
		for (int i = 0; i < 10; i++) {
			threads[i] = new Thread(new Calculator(i));
			if(i%2 == 0){
				threads[i].setPriority(Thread.MAX_PRIORITY);
			}else{
				threads[i].setPriority(Thread.MIN_PRIORITY);
			}
			threads[i].setName("Thread "+i);
		}
		PrintWriter pw = null;
		try {
			FileOutputStream file = new FileOutputStream("d:\\log.txt");
			pw = new PrintWriter(file);
			
			for (int i = 0; i < 10; i++) {
				pw.println("Main:Status of Thread "+i+":"+threads[i].getState());
				status[i] = threads[i].getState();
			}
			
			for (int i = 0; i < 10; i++) {
				threads[i].start();
			}
			
			boolean finish = false;
			while(!finish){
				for (int i = 0; i < 10; i++) {
					if(threads[i].getState() != status[i]){
						writeThreadInfo(pw,threads[i],status[i]);
						status[i] = threads[i].getState();
					}
				}
				finish = true;
				for (int i = 0; i < 10; i++) {
					finish = finish && (threads[i].getState() == State.TERMINATED);
				}
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(pw != null){
				pw.close();
			}
		}
	}
	
	
	
	@Test
	public void testInterrupted(){
		Thread task = new PrimeGenerator();
		task.start();
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		task.interrupt();
		
	}
	
	private void writeThreadInfo(PrintWriter pw,Thread thread,State state){
		pw.printf("Main:Id %d - %s\n",thread.getId(),thread.getName());
		pw.printf("Main: Priority: %d\n", thread.getPriority());
		pw.printf("Main: Old State: %s\n", state);
		pw.printf("Main: New State: %s\n", thread.getState());
		pw.printf("Main : $$$$$$$$$$$$$$$$$$$$$$$$$$\n");
	}
	
}



class Calculator implements Runnable{
	private int number;
    
	public Calculator(int number) {
		this.number = number;
	}
	
	@Override
	public void run() {
		for (int i = 0; i < 10; i++) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName()+" "+number+" "+i+" "+" "+i*number);	
		}
	}
}

class Calculator2 extends Thread{
	private int number;
    
	public Calculator2(int number) {
		this.number = number;
	}
	
	@Override
	public void run() {
		for (int i = 0; i < 10; i++) {
		System.out.println(Thread.currentThread().getName()+" "+number+" "+i+" "+" "+i*number);	
		}
	}
}

class PrimeGenerator extends Thread{
	@Override
	public void run() {
		long number = 1L;
		while(true){
			try {
				if(!isInterrupted())
					Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("print out number:"+number);
			if(isInterrupted()){
				System.out.println("The Prime Generator has benn Interrupted.");
				return;
			}
			number ++;
		}
	}
}
